#pragma once

namespace wjc
{
	template<class T>
	struct less
	{
		bool operator()(const T& left, const T& right)
		{
			return left < right;
		}
	};
	template<class T>
	struct greater
	{
		bool operator()(const T& left, const T& right)
		{
			return left > right;
		}
	};
	template<class T,class Container=vector<T>,class Compare=less<T>>
	class priority_queue
	{
	public:
		priority_queue()
			:_con()
		{
		};
		template<class Iterator>
		priority_queue(Iterator first, Iterator last)
			:_con(first, last)
		{
			int count = _con.size();
			int root = ((count - 1 - 1) / 2);
			while (root >= 0)
			{
				AdjustDown(root);
				root--;
			}
		}


		void push(const T& data)
		{
			_con.pushback(data);
			AdjustUp(_con.size() - 1);
		}

		void pop()
		{
			if (empty())
				return;
			swap(_con.front(), _con.back());
			_con.pop_back();
			AdjustDown(0);
		}

		bool empty()
		{
			return _con.empty();
		}

		size_t size()const
		{
			return _con.size();
		}

		const T& top()const
		{
			return _con.front();
		}

		void AdjustDown(int parent)
		{
			int child = parent * 2 + 1;
			while (child < _con.size())
			{
				if (child + 1 < _con.size() && Compare()(_con[child], _con[child + 1]))
					child++;
				if (Compare()(_con[parent], _con[child]))
				{
					swap(_con[parent], _con[child]);
					parent = child;
					child = parent * 2 + 1;
				}
				else
					break;
			}

		}

		void AdjustUp(int child)
		{
			int parent = (child - 1) / 2;
			while (child >= 0)
			{
				if (Compare()(_con[parent], _con[child]))
				{
					swap(_con[parent], _con[child]);
					child = parent;
					parent = (child - 1) / 2;
				}
				else
					break;
			}
		}

	private:
		Container _con;
	};




}